<template>
	<div class="content">
		<div class="add" style="margin-top: 10px;">
			<Button type="primary" class="fr" @click="add">新增发布</Button>
		</div>
		<Table stripe :columns="columns" ref="table" :data='list' :loading="loading"></Table>
		<Modal v-model="addModal" title="新增发布" width="40%" :mask-closable="false">
			<Tabs v-model="tabsValue">
				<TabPane label="发布出售" name="buy">
					<Form :label-width="100" :model="buyForm" :rules="rules" ref="buyForm">
						<FormItem label="价格" prop="price">
							<Input class="w-50" v-model="buyForm.price" placeholder="请输入价格" />
						</FormItem>
						<FormItem label="发布数量" prop="num">
							<Input class="w-50" v-model="buyForm.num" placeholder="请输入发布数量" />
						</FormItem>
					</Form>
				</TabPane>
				<TabPane label="发布收购" name="sell">
					<Form :label-width="100" :model="sellForm" :rules="rules2" ref="sellForm">
						<FormItem label="价格" prop="price">
							<Input class="w-50" v-model="sellForm.price" placeholder="请输入价格" />
						</FormItem>
						<FormItem label="发布数量" prop="num">
							<Input class="w-50" v-model="sellForm.num" placeholder="请输入发布数量" />
						</FormItem>
					</Form>
				</TabPane>
			</Tabs>
			<div slot="footer">
				<Button type="text" size="large" @click="closeAdd">取消</Button>
				<Button type="primary" size="large" @click="addSubmit">确定</Button>
			</div>
		</Modal>
		<Modal v-model="updateModal" title="修改发布" width="40%" :mask-closable="false">
			<Form :label-width="100" :model="updateForm" :rules="rules3" ref="updateForm">
				<FormItem label="价格" prop="price">
					<Input class="w-50" v-model="updateForm.price" placeholder="请输入价格" />
				</FormItem>
				<FormItem label="发布数量" prop="num">
					<Input class="w-50" v-model="updateForm.num" placeholder="请输入发布数量" />
				</FormItem>
			</Form>
			<div slot="footer">
				<Button type="text" size="large" @click="closeUpdate">取消</Button>
				<Button type="primary" size="large" @click="updateSubmit">确定</Button>
			</div>
		</Modal>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				loading: true,
				list: [], // 分享收益列表数据
				dataValue: ['',''], // 时间段值
				addModal: false,
				updateModal: false,
				tabsValue: 'buy',
				updateId: '',
				updateTypeId: '',
				list2: [],
				state: '-1',
				rules: {
					price: [{
						required: true,
						message: "请输入价格",
						trigger: "blur"
					}],
					num: [{
						required: true,
						message: "请输入发布数量",
						trigger: "blur"
					}]
				},
				buyForm: {
					price: '',
					minNum: '',
					maxNum: '',
					num: '',
				},
				rules2: {
					price: [{
						required: true,
						message: "请输入价格",
						trigger: "blur"
					}],
					num: [{
						required: true,
						message: "请输入发布数量",
						trigger: "blur"
					}]
				},
				sellForm: {
					price: '',
					minNum: '',
					maxNum: '',
					num: '',
				},
				rules3: {
					price: [{
						required: true,
						message: "请输入价格",
						trigger: "blur"
					}],
					num: [{
						required: true,
						message: "请输入发布数量",
						trigger: "blur"
					}]
				},
				updateForm: {
					price: '',
					minNum: '',
					maxNum: '',
					num: '',
				},
				columns: [{
						title: '发布时间',
						align: 'center',
						key: 'createTime',
						sortable: true,
						render: (h, params) => {
							return h('div', params.row.createTime == null ? '#' : this.stringToTime(params.row.createTime))
						}
					}, {
						title: '价格',
						align: 'center',
						key: 'price',
						render: (h, params) => {
							return h('div', params.row.price == null ? '0.00' : this.Fixed(params.row.price));
						}
					},
					{
						title: '库存',
						align: 'center',
						key: 'num',
						render: (h, params) => {
							return h('div', params.row.num == null ? '0.00' : this.Fixed(params.row.num));
						}
					},
					{
						title: '发布类型',
						align: 'center',
						key: 'typeId',
						render: (h, params) => {
							return h('div', params.row.typeId == null ? '#' : params.row.typeId == '1' ? '出售' : params.row.typeId == '2' ? '收购' : '#')
						}
					},
					{
						title: '产品信息',
						align: 'center',
						key: 'dealNum',
						render: (h, params) => {
							return h('div', params.row.currencies == null ? '#' : params.row.currencies == 1 ? 'ATM' : params.row.currencies == 2 ? 'USDT' : '#')
						}
					},
					{
						title: '等级',
						align: 'center',
						key: 'flag',
						render: (h, params) => {
							return h('div', params.row.flag == null ? '0.00' : params.row.flag)
						}
					},
					{
						title: '操作',
						align: 'center',
						render: (h, params) => {
							return h('div', [
								h('Button', {
									props: {
										type: 'primary',
										size: 'small'
									},
									style: {
										marginRight: '5px'
									},
									on: {
										click: () => {
											this.updateModal = true;
											this.axios.get('/admin/trading/trades/' + params.row.id).then(res => {
												if(res.data.code == 200) {
													this.$refs["updateForm"].resetFields();
													this.updateId = params.row.id;
													this.updateTypeId = params.row.typeId,
														this.updateForm = res.data.data;
												}
											})
										}
									}
								}, '修改')
							])
						}
					}
				],
			}
		},
		created() {

		},
		mounted() {
			this.$Message.config({
				top: 250
			})
			this.getQuery() // 查询直推奖励
		},
		methods: {
			getQuery() {
				this.loading = true;
				this.axios.get('/admin/trading/trades').then(res => {
					if(res.data.code == 200) {
						this.list = res.data.data;
						this.loading = false;
					}
				})
			},
			cuctGetQuery(page) { // 分页查询直推奖励
				this.loading = true;
				this.axios.get('/admin/trading/trades').then(res => {
					if(res.data.code == 200) {
						this.list = res.data.data;
						this.loading = false;
					}
				})
			},
			add() {
				this.addModal = true;
				this.$refs["buyForm"].resetFields();
				this.$refs["sellForm"].resetFields();
			},
			closeAdd() {
				this.$refs["buyForm"].resetFields();
				this.$refs["sellForm"].resetFields();
				this.addModal = false;
			},
			closeUpdate() {
				this.$refs["updateForm"].resetFields();
				this.updateModal = false;
			},
			updateSubmit() {
				this.$refs.updateForm.validate(valid => {
					if(valid) {
						this.axios.put('/admin/trading/release', this.qs.stringify({
							id: this.updateId,
							typeId: this.updateTypeId,
							price: this.updateForm.price,
							num: this.updateForm.num,
						})).then(res => {
							if(res.data.code == 200) {
								this.getQuery();
								this.closeUpdate();
							}
						})
					}
				})
			},
			addSubmit() {
				if(this.tabsValue == 'buy') {
					this.$refs.buyForm.validate(valid => {
						if(valid) {
							this.axios.post('/admin/trading/release', this.qs.stringify({
								price: this.buyForm.price,
								num: this.buyForm.num,
								typeId: 1,
							})).then(res => {
								if(res.data.code == 200) {
									this.getQuery();
									this.$Message.info(res.data.msg);
									this.closeAdd();
								}
							})
						}
					})
				} else if(this.tabsValue == 'sell') {
					this.$refs.sellForm.validate(valid => {
						if(valid) {
							this.axios.post('/admin/trading/release', this.qs.stringify({
								price: this.sellForm.price,
								num: this.sellForm.num,
								typeId: 2,
							})).then(res => {
								if(res.data.code == 200) {
									this.getQuery();
									this.$Message.info(res.data.msg);
									this.closeAdd();
								}
							})
						}
					})
				}
			},
		}
	}
</script>

<style>

</style>